package View;

import Domain.House;
import Service.HouseService;
import Utility.Utility;

/*
1.显示界面
2.接收用户输入
3.调用HouseService完成对房屋信息的各种操作
 */
public class HouseView {
    private int choice;
    private boolean loop = true;
    private HouseService houseService = new HouseService(10);

    public void MainMenu() {
        //显示主菜单
        do {
            System.out.println("==============房屋出租系统==============");
            System.out.println("\t\t\t1. 新 增 房 源");
            System.out.println("\t\t\t2. 查 找 房 屋");
            System.out.println("\t\t\t3. 删除房屋信息");
            System.out.println("\t\t\t4. 修改房屋信息");
            System.out.println("\t\t\t5. 显示房屋信息");
            System.out.println("\t\t\t6. 退      出");

            System.out.print("请输入:>");
            choice = Utility.readInt();

            switch (choice) {
                case 1:
                    AddHouse();
                    break;
                case 2:
                    SearchHouse();
                    break;
                case 3:
                    DelHouse();
                    break;
                case 4:
                    UpdateHouse();
                    break;
                case 5:
                    listHouse();
                    break;
                case 6:
                    //使用utilityt自带的选择方法
                    char c = Utility.readConfirmSelection();
                    if (c == 'Y') {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("请输入正确的选择!");
                    break;
            }

        } while (loop);


    }


    //新增房源AddHouse()
    public void AddHouse() {
        System.out.println("==========新增房源==========");
        System.out.print("姓名 :");
        String name = Utility.readString(6);
        System.out.print("电话 :");
        String phone = Utility.readString(12);
        System.out.print("地址 :");
        String address = Utility.readString(20);
        System.out.print("月租 :");
        int rent = Utility.readInt(7);
        System.out.print("状态 :");
        String state = Utility.readString(3);
        House house = new House(houseService.getCount() + 1, name, phone, address, rent, state);
        if (houseService.add(house)) {
            System.out.println("新增房源成功!");
        } else {
            System.out.println("新增房源失败!");
        }
    }

    //查找房屋信息·
    public void SearchHouse() {
        System.out.println("==========查找房屋信息==========");
        System.out.print("请输入你要查找的房屋id:>");
        int delId = Utility.readInt();
        House house = houseService.search(delId);
        if (house != null) {
            System.out.println("查询成功，房屋信息如下:>");
            System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(出租/未出租)");
            System.out.println(house);
        } else {
            System.out.println("未查询到此房屋信息!");
        }

    }


    //删除房屋信息DelHouse
    public void DelHouse() {
        System.out.println("==========删除房屋信息==========");
        System.out.print("请输入要删除的房屋编号(-1退出):>");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("放弃删除");
            return;
        }
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } else {
            System.out.println("放弃删除");
            return;
        }
    }

    //修改房屋信息
    public void UpdateHouse() {
        System.out.println("==========修改房屋信息==========");
        System.out.print("请选择待修改房屋信息的编号(-1退出):>");
        int updateId = Utility.readInt();
        if (updateId == -1) {
            System.out.println("你放弃了修改房屋信息..");
            return;
        }

        House house = houseService.search(updateId);//返回的是引用类型，可以直接修改
        if (house == null) {
            System.out.println("你要修改的房屋信息不存在!");
            return;
        }

        System.out.print("姓名(" + house.getName() + "):");
        String name = Utility.readString(8, "");//如果用户直接按回车则表示不修改该信息，默认""
        if (!name.equals("")) {
            house.setName(name);
        }

        System.out.print("电话(" + house.getPhone() + "):");
        String phone = Utility.readString(12, "");
        if (!phone.equals("")) {
            house.setPhone(phone);
        }

        System.out.print("地址(" + house.getAddress() + "):");
        String address = Utility.readString(18, "");
        if (!address.equals("")) {
            house.setAddress(address);
        }

        System.out.print("租金(" + house.getRent() + "):");
        int rent = Utility.readInt(-1);//如果直接回车则返回-1
        if (rent != -1) {
            house.setRent(rent);
        }

        System.out.print("状态(" + house.getState() + "):");
        String state = Utility.readString(3, "");
        if (!state.equals("")) {
            house.setState(state);
        }

        System.out.println("修改成功!");

    }


    //显示房屋信息
    public void listHouse() {
        System.out.println("========================房屋列表========================");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(出租/未出租)");
        House house[] = houseService.list();
        for (int i = 0; i < house.length; i++) {
            if (house[i] != null) {
                System.out.println(house[i]);
            }
        }
        System.out.println("======================================================");

    }


}
